package com.pactera.miyuangroup.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.pactera.miyuangroup.config.CommonConfig;
import com.pactera.miyuangroup.service.MiyuanDataService;
import com.pactera.miyuangroup.util.OkHttpUtils;
import com.pactera.miyuangroup.vo.response.miyuan.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 米源数据
 *
 * @author Wangwu
 * @create 2018-04-25 下午1:39
 **/
@Service
@Slf4j
public class MiyuanDataServiceImp implements MiyuanDataService {

    @Autowired
    private CommonConfig common;

    /**
     * 商品分类
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<Classification>> getGoodsClassification(int pageNumber, int pageSize) {
        Map<String, String> queries = new HashMap<>();
        queries.put("pageNumber", String.valueOf(pageNumber));
        queries.put("pageSize", String.valueOf(pageSize));
        log.info("商品分类查询");
        String data = OkHttpUtils.get(common.getOkHttp().getClassificationUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<Classification>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<Classification>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }


    /**
     * 商品
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<GoodsInfo>> getGoodsInfo(int pageNumber, int pageSize) {
        Map<String, String> queries = new HashMap<>();
        queries.put("pageNumber", String.valueOf(pageNumber));
        queries.put("pageSize", String.valueOf(pageSize));
        log.info("商品信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getGoodsUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<GoodsInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<GoodsInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 组织机构
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<Organization>> getOrganizationInfo(int pageNumber, int pageSize) {
        Map<String, String> queries = new HashMap<>();
        queries.put("pageNumber", String.valueOf(pageNumber));
        queries.put("pageSize", String.valueOf(pageSize));
        log.info("组织机构信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getOrganizationUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<Organization>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<Organization>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 员工信息
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<UserInfo>> getUserInfo(int pageNumber, int pageSize) {
        Map<String, String> queries = new HashMap<>();
        queries.put("pageNumber", String.valueOf(pageNumber));
        queries.put("pageSize", String.valueOf(pageSize));
        log.info("员工信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getUserUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<UserInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<UserInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 车辆信息
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<VehicleInfo>> getVehicleInfo(int pageNumber, int pageSize) {
        Map<String, String> queries = new HashMap<>();
        queries.put("pageNumber", String.valueOf(pageNumber));
        queries.put("pageSize", String.valueOf(pageSize));
        log.info("车辆信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getVehicleUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<VehicleInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<VehicleInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 车辆售货机查询
     *
     * @param licensePlate 车牌号
     * @return
     */
    @Override
    public MiYuanModel<List<VendorInfo>> getVendorInfo(String licensePlate) {
        Map<String, String> queries = new HashMap<>();
        queries.put("licensePlate", licensePlate);
        log.info("车辆售货机信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getVendorUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<List<VendorInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<List<VendorInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 查询售货机货道信息
     *
     * @param number 售货机编号
     * @return
     */
    @Override
    public MiYuanModel<List<VendorPassagewayInfo>> getVendorPassagewayInfo(String number) {
        Map<String, String> queries = new HashMap<>();
        queries.put("number", number);
        log.info("售货机货道信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getVendorPassagewayUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<List<VendorPassagewayInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<List<VendorPassagewayInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }


    /**
     * 查询售货机货道信息(增量)
     *
     * @param day 查询时间（日）
     * @return
     */
    @Override
    public MiYuanModel<List<VendorPassagewayIncrementInfo>> getVendorPassagewayIncrementInfo(Date day) {
        Map<String, String> queries = new HashMap<>();
        queries.put("chrecordTime", new com.pactera.miyuangroup.util.Date(day).toShortDate());
        log.info("售货机货道信息增量查询");
        String data = OkHttpUtils.get(common.getOkHttp().getVendorPassagewayIncrementUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<List<VendorPassagewayIncrementInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<List<VendorPassagewayIncrementInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }


    /**
     * 查询售货机货道售卖信息
     *
     * @param number           售货机编号
     * @param passagewayNumber 货道编号
     * @return
     */
    @Override
    public MiYuanModel<VendorPassagewaySaleInfo> getVendorPassagewaySaleInfo(String number, String passagewayNumber) {
        Map<String, String> queries = new HashMap<>();
        queries.put("number", number);
        queries.put("passagewayNumber", passagewayNumber);
        log.info("查询售货机货道售卖信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getSaleUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<VendorPassagewaySaleInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<VendorPassagewaySaleInfo>>>() {
            });
            if (null == miyuan || !miyuan.isSuccess() || null == miyuan.getData() || miyuan.getData().getRecords().size() <= 0) {
                return MiYuanModel.fail();
            } else {
                VendorPassagewaySaleInfo info = miyuan.getData().getRecords().stream().findFirst().orElse(null);
                if (null == info) {
                    return MiYuanModel.fail();
                } else {
                    MiYuanModel<VendorPassagewaySaleInfo> rinfo = new MiYuanModel<VendorPassagewaySaleInfo>();
                    rinfo.setMsg(miyuan.getMsg());
                    rinfo.setSuccess(miyuan.isSuccess());
                    rinfo.setCode(miyuan.getCode());
                    rinfo.setData(info);
                    return rinfo;
                }
            }
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 查询售货机货道售卖信息
     *
     * @param number 售货机编号
     * @return
     */
    @Override
    public MiYuanModel<MiYuanPage<VendorPassagewaySaleInfo>> getVendorPassagewaySaleInfo(String number) {
        Map<String, String> queries = new HashMap<>();
        queries.put("number", number);
        log.info("查询售货机货道售卖信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getSaleUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<MiYuanPage<VendorPassagewaySaleInfo>> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<MiYuanPage<VendorPassagewaySaleInfo>>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 查询售货机销售金额
     *
     * @param controlNumber 售货机工控编号
     * @return
     */
    @Override
    public MiYuanModel<VendorSaleInfo> getVendorSaleInfo(String controlNumber) {
        Map<String, String> queries = new HashMap<>();
        queries.put("controlNumber", controlNumber);
        log.info("查询售货机销售金额信息查询");
        String data = OkHttpUtils.get(common.getOkHttp().getSalesAmountUrl(), queries);
        log.info("解析JSON");
        try {
            MiYuanModel<VendorSaleInfo> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<VendorSaleInfo>>() {
            });
            if (null == miyuan || !miyuan.isSuccess()) {
                return MiYuanModel.fail();
            } else {
                if (StringUtils.isNotBlank(miyuan.getData().getMoneyCash())) {
                    miyuan.getData().setTotalAmount(miyuan.getData().getMoneyCash());
                } else {
                    miyuan.getData().setTotalAmount("0");
                }
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }

    /**
     * 补货完成
     *
     * @param info 补货信息
     * @return
     */
    @Override
    public MiYuanModel<VendorSaleInfo> postReplenishmentInfo(ReplenishmentInfo info) {
        Map<String, String> params = new HashMap<>();
        params.put("controlNumber", info.getControlNumber());
        params.put("replenishmentTime", info.getReplenishmentTime());
        log.info("补货完成");
        String data = OkHttpUtils.post(common.getOkHttp().getReplenishmentUrl(), params, "utf-8");
        log.info("解析JSON");
        try {
            MiYuanModel<VendorSaleInfo> miyuan = JSON.parseObject(data, new TypeReference<MiYuanModel<VendorSaleInfo>>() {
            });
            if (null == miyuan) {
                return MiYuanModel.fail();
            }
            return miyuan;
        } catch (Exception e) {
            log.info("解析JSON 异常：{}", e.getMessage());
            return MiYuanModel.fail();
        }
    }


}
